package io.renren.modules.sys.controller;

import java.util.Arrays;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.entity.UserOrderLogEntity;
import io.renren.modules.sys.service.SysUserService;
import io.renren.modules.sys.service.UserAmountService;
import io.renren.modules.sys.service.UserOrderLogService;



/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-12-15 10:27:12
 */
@RestController
@RequestMapping("sys/userorderlog")
public class UserOrderLogController extends AbstractController{
    @Autowired
    private UserOrderLogService userOrderLogService;
    
    @Autowired
    private UserAmountService userAmountService;
    
    @Autowired
    private SysUserService sysUserService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:userorderlog:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userOrderLogService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:userorderlog:info")
    public R info(@PathVariable("id") Integer id){
        UserOrderLogEntity userOrderLog = userOrderLogService.selectById(id);

        return R.ok().put("userOrderLog", userOrderLog);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:userorderlog:save")
    public R save(@RequestBody UserOrderLogEntity userOrderLog){
        userOrderLogService.insert(userOrderLog);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:userorderlog:update")
    public R update(@RequestBody UserOrderLogEntity userOrderLog){
        ValidatorUtils.validateEntity(userOrderLog);
        userOrderLogService.updateAllColumnById(userOrderLog);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:userorderlog:delete")
    public R delete(@RequestBody Integer[] ids){
        userOrderLogService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }
    
    /**
     * 审核用户提现申请
     */
    @RequestMapping("/recheckApply")
    @RequiresPermissions("sys:userorderlog:update")
    public R recheckApply(@RequestBody UserOrderLogEntity userOrderLog){
    	userOrderLog.setUpdateBy(String.valueOf(getUser().getUserId()));
    	int updateCount = userOrderLogService.recheckApply(userOrderLog);
    	if(updateCount > 0 && "1".equals(userOrderLog.getStatus())){
    		//增加提现金额
    		userAmountService.updateUseAmount(userOrderLog);
    	}
    	
        return R.ok();
    }
    
    /**
     * 审核用户认证
     */
    @RequestMapping("/recheckAuth")
    @RequiresPermissions("sys:userorderlog:update")
    public R recheckAuth(@RequestBody UserOrderLogEntity userOrderLog){
    	int updateCount = userOrderLogService.recheckAuth(userOrderLog);
    	if(updateCount > 0 && "1".equals(userOrderLog.getStatus())){
    		sysUserService.authUser(userOrderLog.getUserId());
    	}
    	
        return R.ok();
    }

}
